#ifndef QABSTRACTDRIVER_H
#define QABSTRACTDRIVER_H

#include "../host/qabstracthost.h"

#include <QMap>
#include <QDateTime>
#include <QTimer>

enum  enDataTpye
{
    DT_READ,
    DT_WRITE
};

class QMissionManager;
class QMission;

struct tagDriverDataInfo
{
    QString     m_uuid;
    QString     m_name;
    QString     m_data_uuid;
    QString     m_information;
    int         m_data_address;
    int         m_period;
    int         m_temp_data;
    int         m_level;
    int         m_scale;
    int         m_machine_address;
    enDataTpye  m_type;
    tagDriverDataInfo()
    {
        m_uuid=m_name=m_data_uuid=m_information="";
        m_data_address=0;
        m_period=1000;
        m_temp_data=0;
        m_level=10;
        m_scale=1;
        m_machine_address=1;
        m_type=DT_READ;
    }
};

class SHAREDLIB_EXPORT QAbstractDriver : public QAbstractHost
{
    Q_OBJECT
public:
    QAbstractDriver(QAbstractHost *parent = 0);
    ~QAbstractDriver();

    bool    load(const QString &path);
    void    save(const QString &path);

    QList<tagDriverDataInfo*>   get_all_data();
    tagDriverDataInfo*          get_data(const QString &uuid);

    void                        insert_data(const tagDriverDataInfo& data,int index=-1);
    void                        remove_data(const QString &uuid);

    QString                     get_name();
    void                        set_name(const QString &name);
    QStringList                 get_all_data_names();

    static QString get_show_group();

    void                        changed_data(const QString &uuid,const QString &key,const QVariant &value);

public:
    virtual void    start()=0;
    virtual void    closed(){}
    virtual void    update_send_data(QMission *ms);
protected:
    virtual void    write(const QByteArray &data);

    void    online_exec();
    void    offline_exec();
    void    error_exec(const QString &msg);

    void    report_communicate(int type,const QByteArray &data);
signals:
    void    insert_data_signals(tagDriverDataInfo* data,int index);
    void    remove_data_signals(tagDriverDataInfo* data);
    void    data_refresh(tagDriverDataInfo *data);

    void    online();
    void    offline();

    void    open();
    void    close();

    void    report(const QString &msg);
protected slots:
    void    run();
protected:
    static ushort get_crc(uchar* buffer,int size);
    void init_property();
protected:
    QList<tagDriverDataInfo*>       m_datas;
    QMap<QString,tagDriverDataInfo*>    m_uuid_to_data;
    QString                         m_name;

    QMissionManager                 *m_mission_manager;
    QString                         m_current_mission_id;
    QDateTime                       m_last_time;
    int                             m_retry;
    QTimer                          *m_timer;
};

#endif // QABSTRACTDRIVER_H
